package net.xdclass.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import net.xdclass.entity.Address;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.request.AddressRequest;
import net.xdclass.service.IAddressService;
import net.xdclass.utils.JsonData;
import net.xdclass.vo.AddressVO;
import org.springframework.web.bind.annotation.*;

import springfox.documentation.spring.web.json.Json;

import java.util.List;

/**

 * <p>
 * 电商-公司收发货地址表 前端控制器
 * </p>
 *
 * @author lixin
 * @since 2024-08-14
 */
@RestController
@RequestMapping("/api/address/v1")
@RequiredArgsConstructor
@Api(tags = "收货地址模块")
public class AddressController {

    private final IAddressService addressService;

    /**
     * 根据id查找收货地址
     * @param addressId
     * @return
     */
    @ApiOperation("根据id查找地址详情")
    @GetMapping("/find/{address_id}")
    public JsonData detail(@ApiParam(value = "地址id", required = true)
                             @PathVariable("address_id")  long addressId){
        AddressVO addressVO = addressService.detail((int) addressId);
        return addressVO == null ? JsonData.buildResult(BizCodeEnum.ADDRESS_NO_EXITS) : JsonData.buildSuccess(addressVO);
    }

    /**
     * 新增收货地址
     * @param addressRequest
     * @return
     */
    @ApiOperation("新增收货地址")
    @PostMapping("/add")
    public JsonData add(@ApiParam("地址对象") @RequestBody AddressRequest addressRequest){
        addressService.add(addressRequest);

        return JsonData.buildSuccess();
    }

    /**
     * 删除指定收货地址
     * @param addressId
     * @return
     */
    @ApiOperation("删除收货地址")
    @DeleteMapping("/del/{address_id}")
    public  JsonData del(@PathVariable("address_id") int addressId){

        int rows = addressService.del(addressId);

        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.ADDRESS_DEL_FAIL);
    }

    /**
     * 查询用户全部收货地址
     * @return
     */
    @ApiOperation("查询用户全部收货地址")
    @GetMapping("/list")
    public JsonData findAllAddress(){

        List<AddressVO> list = addressService.listUserAllAddress();

        return JsonData.buildSuccess(list);
    }
}
